﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.DTO.Web.Login;
using MKAdminCore.IService.Web.Login;
using MKAdminCore.ToolKit;
using MKAdminCore.ToolKit.Encrypt;
using Dapper;
using Newtonsoft.Json;

namespace MKAdminCore.ServiceImpl.Web.Login
{
    public class LoginService : ILoginService
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Result<UserInfo> UserLogin(LoginInfoParameter parameter)
        {
            var result = new Result<UserInfo>()
            {
                status = false,
                data = new UserInfo()
            };
            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                string sql = @"SELECT top 1 oi.OperatorId OperatorId,oi.OperatorNo,oi.OperatorName
                                FROM DBO.OperatorInfo oi With(NoLock)
                               Where oi.OperatorNo = @OperatorNo And oi.OperatorPwd = @OperatorPwd
                                    And oi.StatusCode = 0";

                var param = new DynamicParameters();
                param.Add("@OperatorNo", parameter.OperatorNo);
                param.Add("@OperatorPwd", MD5EncryptHelper.Encrypt(parameter.OperatorPwd));

                var userInfo = conn.QueryFirstOrDefault<UserInfo>(sql, param);
                if (userInfo != null && userInfo.OperatorId > 0)
                {
                    result.status = true;
                    result.data = userInfo;
                }

            }

            return result;
        }

        /// <summary>
        /// 用户登录日志
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public async Task<Result> UserLoginLog(string operatorNo, bool loginStatus, string ipAddress)
        {
            var result = new Result();

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine($@"Insert Into dbo.LoginLog(OperatorNo,LoginStatus,IPAddress,LoginTime)
                                  Values(@OperatorNo,@LoginStatus,@IPAddress,GetDate())");

                var param = new DynamicParameters();
                param.Add("@OperatorNo", operatorNo);
                param.Add("@LoginStatus", loginStatus);
                param.Add("@IPAddress", ipAddress);

                await conn.ExecuteAsync(sql.ToString(), param);

                return result;
            }
        }

        /// <summary>
        /// 验证用户状态
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Result<UserInfo> VerifyUserStatus(UserInfo userInfo)
        {
            var result = new Result<UserInfo>()
            {
                status = false,
                data = new UserInfo()
            };

            return result;
        }
    }
}
